package com.cb.spzx.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.spzx.manager.service.CategoryBrandService;
import com.cb.spzx.model.dto.product.CategoryBrandDto;
import com.cb.spzx.model.entity.product.CategoryBrand;
import com.cb.spzx.model.vo.common.Result;
import com.cb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "分类品牌管理")
@RestController
@RequestMapping(value = "/admin/product/categoryBrand")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Operation(summary = "分页查询分类品牌")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer limit,
                             CategoryBrandDto categoryBrandDto){
        System.out.println("page = " + page + ", limit = " + limit + ", categoryBrandDto = " + categoryBrandDto);
        Page<CategoryBrand> categoryBrandPage = new Page<>(page, limit);
        categoryBrandService.findByPage(categoryBrandPage,categoryBrandDto);
        return Result.build(categoryBrandPage, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加分类品牌")
    @PostMapping("/save")
    public Result saveCategoryBrand(@RequestBody CategoryBrand categoryBrand){
        categoryBrandService.saveCategoryBrand(categoryBrand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改分类品牌")
    @PutMapping("updateById")
    public Result updateById(@RequestBody CategoryBrand categoryBrand){
        categoryBrand.setUpdateTime(new Date());
        categoryBrandService.updateById(categoryBrand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        categoryBrandService.removeById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }
}
